package com.java.comprehensive.design.structural_model.flyweight_pattern;

import java.util.HashMap;

/**
 * 工厂类 将该类设计为单例
 */
public class BoxFactory {
    private HashMap<String, AbstractBox> map;

    private static final BoxFactory boxFactory;

    //在静态代码块中复制，静态代码块先加载
    static {
        boxFactory = new BoxFactory();
    }

    //构造方法初始化
    private BoxFactory() {
        this.map = new HashMap<>();
        map.put("I", new IBox());
        map.put("L", new LBox());
    }

    //提供单例方法
    public static BoxFactory getInstance() {
        return boxFactory;
    }

    //根据名称获取图形对象
    public AbstractBox getShape(String name) {
        return map.get(name);
    }
}
